package com.dlc.apk.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.dlc.apk.utils.DeviceUtil
import com.dlc.apk.utils.LogPlus

/**
 * @author We
 */
abstract class BaseFragment : Fragment()/*, View.OnClickListener*/ {
    private var viewDataBinding: ViewDataBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LogPlus.e("### onCreate,fragment->" + this.javaClass.simpleName)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        LogPlus.e("### onCreateView, fragment->" + this.javaClass.simpleName)
        viewDataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)
        viewDataBinding!!.lifecycleOwner = this
        //进行UI model 实例化
        initViewModels()
        //进行业务层Service初始化
        initServices()
        viewDataBinding!!.root.isClickable = true
        return viewDataBinding!!.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        init()
        LogPlus.e("###onActivityCreated, fragment->" + this.javaClass.simpleName)
        super.onActivityCreated(savedInstanceState)
        //LogPlus.e("###topFragment -> ${getParentActivity()?.topFragment?.javaClass?.simpleName}")
    }

    protected abstract fun getLayoutId(): Int

    /**
     * 实例化双向数据绑定需要用到的models
     */
    protected abstract fun initViewModels()

    /**
     * 实例化业务层Service
     */
    protected abstract fun initServices()
    protected abstract fun init()
    fun <T> getViewDataBinding(): T? {
        return viewDataBinding as T?
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        LogPlus.e("###onAttach,fragment->" + javaClass.simpleName)
    }

    override fun onAttachFragment(childFragment: Fragment) {
        super.onAttachFragment(childFragment)
        LogPlus.e("#onAttachFragment,fragment->" + javaClass.simpleName)
    }

    override fun onDetach() {
        super.onDetach()
        LogPlus.e("###onDetach,fragment->" + javaClass.simpleName)
    }

    override fun onPause() {
        super.onPause()
        LogPlus.e("###onPause,fragment->" + javaClass.simpleName)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        LogPlus.e("###onDestroyView,fragment->" + javaClass.simpleName)
    }

    override fun onDestroy() {
        super.onDestroy()
        LogPlus.e("###onDestroy,fragment->" + javaClass.simpleName)
    }

    fun getParentActivity(): BaseActivity? {
        if (activity is BaseActivity) {
            return activity as BaseActivity?
        }
        return null
    }

    fun showProgressDialog(msg: String?) {
        val baseActivity = getParentActivity() ?: return
        baseActivity.runOnUiThread { baseActivity.showProgressDialog(msg) }
    }

    fun dismissProgressDialog() {
        val baseActivity = getParentActivity() ?: return
        baseActivity.runOnUiThread { baseActivity.dismissProgressDialog() }
    }

    fun showToast(msg: String?) {
        val baseActivity = getParentActivity() ?: return
        baseActivity.runOnUiThread { baseActivity.showOneToast(msg) }
    }

    override fun onResume() {
        super.onResume()
        LogPlus.e("###onResume fragment->" + javaClass.simpleName)
    }

    fun onResume2() {
        LogPlus.e("###onResume2 fragment->" + javaClass.simpleName)
    }

    /*override fun onClick(p0: View?) {
        
    }*/

    override fun onHiddenChanged(hidden: Boolean) {
        if (hidden) {
            if (activity != null) {
                DeviceUtil.hideSoftInput(activity!!)

                if (activity is BaseActivity) {
                    (activity!! as BaseActivity).dismissProgressDialog()
                }
            }
        }
    }
}